// 03myCenter/02sectorization/01myChange/widthdraw/widthdraw.js
var co = require('../../../../resource/js/common.js')
var Md5 = require('../../../../resource/js/md5.js')
//var details = [], pageNum = 1;
var app = getApp()
var _this, _data

Page({
   data: {
      money: '', //余额
      allDeposit: false, //是否全部提现
      overtop: false, //提现金额是否超出余额
      // bankList: [{
      //    "Bank_Account_ID": 6,
      //    "Bank_Name": "微信零钱包",
      //    "Account_No": "",
      // }], //已绑定的银行卡列表
      bankList: [],
      inputMoney: '', //输入的金额
      modal: false, //选择银行卡模态框
      modal_pay: false, //支付模态框
      indexs: 0, //所选银行卡信息    
      inputMoney: '', //输入的金额
      inputStr: '', //输入金额(字符串)
      pay_Fee: '', //手续费
      account_Money: '', //实际到账金额       
   },

   onLoad: function(options) {
      console.log("md5===", Md5.md5(""))
      _this = this
      _data = this.data
      // console.log(options.value)
      // console.log("app.Data.userInfo====", app.Data.userInfo)
      this.setData({
         money: app.Data.userInfo.User_Balance
      })
      console.log('app.Data',app.Data)
      this.getBankList(app.Data.userInfo.User_ID, app.Data.userInfo.ID_Card_No) //获取已绑定银行卡信息
   },

   allWithdraw: function() {
      var pay_fee = _data.money * 0.001 > 1 ? _data.money * 0.001 : 1
      this.setData({
         allDeposit: true,
         inputMoney: _data.money,
         inputStr: co.roundStr(_data.money, 2),
         pay_Fee: co.roundStr(pay_fee, 2),
         account_Money: co.roundStr(_data.money - pay_fee, 2)
      })
   },

   //输入金额
   inputMoney: function (e) {
      var balance = parseFloat(this.data.money);
      var money = parseFloat(e.detail.value);
      var inputStr = e.detail.value
      var pay_fee = money * 0.001 > 1 ? money * 0.001 : 1
      this.setData({
         overtop: money > balance,
         allDeposit: false,
         inputMoney: money,
         inputStr: co.filterStr(inputStr, '.'),
         pay_Fee: co.roundStr(pay_fee, 2),
         account_Money: co.roundStr(money - pay_fee, 2)
      })
   },

   //获取已绑定银行卡信息
   getBankList: function(userID, ID_Card_Name) {
      var url = 'bankcard/ba/getList';
      co.getShareApi(url, {
         User_ID: userID,
         Account_Name: ID_Card_Name
      }, function(res) {
         if (res.code == 0) {
            _this.setData({
               //bankList: _data.bankList.concat(res.data)
               bankList: res.data
            })
            console.log('bankList===', _data.bankList)
         }
      })
   },

   //显示选择提现方式
   select: function() {
      this.setData({
         modal: true
      })
   },

   //关闭选择提现方式
   close: function() {
      this.setData({
         modal: false
      })
   },

   radioChange: function(e) {
      var index = e.detail.value
      console.log(index)
      if (index > -1) {
         this.setData({
            modal: false,
            indexs: e.detail.value
         })
      } else {
         wx.navigateTo({
            url: '/04myCenter/01myWallet/01myChange/bankCard/addBankCard/addBankCard?scene=User_ID.' + app.Data.userInfo.User_ID + '.From_Page.bankCard'
         })
      }
   },

   //提现(显示支付密码模态框)
   submit: function (e) {
      console.log('点提现e===', e)
      if (co.empty(this.data.inputMoney) || this.data.inputMoney <= 0) {
         return
      }
      if (_data.indexs > 0 && _data.inputMoney <= 1) {
         co.confirm('提现到银行卡的金额不能小于1元!',
            '取消', '确定', function (res) {
               _this.setData({
                  allDeposit: false,
                  inputMoney: 0,
                  inputStr: '',
                  pay_Fee: '',
                  account_Money: ''
               })
            })
         return
      }
      //没有真实姓名
      // if (app.Data.userInfo.ID_Card_Name == '') {
      //    co.confirm('请先绑定银行卡认证真实姓名', '好的', '朕知道了', function (res) {
      //       wx.navigateTo({
      //          url: '/04myCenter/01myWallet/01myChange/bankCard/addBankCard/addBankCard',
      //       }) 
      //    })   
      // }
      //提现
      //console.log("sadsaddasdasda====", this.data.overtop)
      if (this.data.overtop) {
         wx.showModal({
            title: '',
            content: '您输入的金额超过您的余额',
         })
      } else {
         console.log('appData==',app.Data)
         this.setData({
            inputMoney: co.round(_data.inputMoney, 2),
            inputStr: co.roundStr(_data.inputMoney, 2),
            modal_pay: true
         })
      }
   },


   //关闭支付密码模态框
   payClose: function() {
      this.setData({
         modal_pay: false
      })
   },

   //提现
   withdraw: function(e) {
      console.log('forName',app.Data)
      
      wx.showLoading({
         title: '支付中',
      })
      this.setData({
         modal_pay: false
      })
      var User_PayPassWord = Md5.md5(e.detail.value.User_PayPassWord)
      var bank = _data.bankList[_data.indexs]
      console.log('_data.indexs===' + _data.indexs)
      if (_data.indexs == 0) {
         //this.readyParam1()
         //this.payToWeixinMoney(User_PayPassWord)
         //console.log("app.Data232323===", app.Data)
         //console.log('this.data=====', _this.data)
         console.log("``````", app.Data.userInfo)
         this.payToWeixinMoney(User_PayPassWord, app.Data.userInfo.Wx_Openid[app.Data.app.appid],app.Data.userInfo.ID_Card_Name)
      } else {
         //console.log("app.Data232323===", app.Data)
         //console.log('this.data=====', _this.data)
         this.payToBank(User_PayPassWord)
      }
   },

   //提现到微信
   payToWeixinMoney: function (User_PayPassWord, open_id, name) {
      var payTo = 'wx_pay'
      co.getShareApi('api/Wxpay/toWxWallet', {
         User_ID: app.Data.userInfo.User_ID,
         User_PayPassword: User_PayPassWord,
         amount: _data.inputMoney,
         desc: '旧约回收小程序提现',
         mch_appid: app.Data.app.appid,
         paySecret: 'bShYjNaUyIzGnEhSiAzIlIsNaHuOhZuF',
         openid: open_id,
         real_name: name,
         mchid: '1504097781',
         Payer: 'User_' + app.Data.userInfo.User_ID,
         Pay_From: '91_pay'
      }, function (res) {
         wx.hideLoading()
         if (res.code == 0) {
            _this.addUserPresentRecord(_data.inputMoney, payTo)
            co.confirm(res.msg, '好的', '朕知道了', function (res) {
               co.navigateBack({}, 1000)
            })
         } else {
            co.Modal(res.msg)
         }
      })
   },

   //提现到银行
   payToBank: function (User_PayPassWord) {
      var payTo = 'bank_card'
      var bank = _data.bankList[_data.indexs]
      var bank_code = bank.Bank_Code
      var bank_Name = bank.Bank_Name
      var enc_bank_no = bank.Account_No
      var enc_true_name = bank.Account_Name
      co.getShareApi('api/Wxpay/toBank', {
         User_ID: app.Data.userInfo.User_ID,
         User_PayPassword: User_PayPassWord,
         mch_appid: app.Data.app.appid,
         paySecret: 'bShYjNaUyIzGnEhSiAzIlIsNaHuOhZuF',
         mch_id: '1504097781',
         bank_code: bank_code,
         amount: _data.inputMoney,
         desc: "91小程序提现",
         enc_bank_no: enc_bank_no,
         enc_true_name: enc_true_name,
         Payer: 'User_' + app.Data.userInfo.User_ID,
         Pay_From: '91_pay'
      }, function (res) {
         wx.hideLoading()
         if (res.code == 0) {
            _this.addUserPresentRecord(_data.inputMoney, payTo)
            // co.confirm(res.msg, '好的', '朕知道了', function (res) {
            //    co.navigateBack({}, 1000)
            // })
         } else {
            co.Modal(res.msg)
         }
      })
   },
   //跳转到忘记支付密码
   retrievePwd: function () {
      wx.navigateTo({
         url: '/04myCenter/02set/02changePayPwd/01retrievePassword/retrievePassword'
      })
   },
   //添加用户提现记录
   addUserPresentRecord: function (money, payTo) {
      var url = 'money/Recorduser/userAdd'
      var param = {
         User_ID: app.Data.userInfo.User_ID,
         Wx_Openid: app.Data.userInfo.Wx_Openid[app.Data.app.appid],
         Abstract: '用户提现',
         Payer: 'User_' + app.Data.userInfo.User_ID,
         Money: money,
         Pay_From: '91_pay',
         Pay_To: payTo,
      }
      co.getJyhsApi(url, param, function (res) {
         if (res.code == 0) {

         }
      })
   },
   // //提现到微信
   // payToWeixinMoney: function(User_PayPassWord) {
   //    var url = "api/Wxpay/toWxWallet"
   //    co.getShareApi(url, {
   //       User_ID: app.Data.userInfo.User_ID,
   //       User_PayPassword: User_PayPassWord,
   //       amount: _data.inputMoney,
   //       desc: '用户-' + app.Data.userInfo.User_NickName + '提现',
   //       mch_appid: 'wxa087ce51079f066e',
   //       paySecret: 'sUiYuAnTeChNoLoGyHtTpSwWwLs293cC',
   //       openid: app.Data.userInfo.Wx_Openid.wxa087ce51079f066e,
   //       real_name: app.Data.userInfo.ID_Card_Name,
   //       mchid: '1437610002'
   //    }, function(res) {
   //       if (res.code == 0) {
   //          _this.readyParam1(-_data.inputMoney)
   //       } else {
   //          wx.hideLoading()
   //       }
   //       co.toastAllText(res.msg)
   //    })
   // },

   // //提现到银行
   // payToBank: function(User_PayPassWord) {
   //    var bank = _data.bankList[_data.indexs]
   //    var bank_code = bank.Bank_Code,
   //       enc_bank_no = bank.Account_No,
   //       enc_true_name = bank.Account_Name;
   //    var url = "api/Wxpay/toBank"
   //    co.getShareApi(url, {
   //       User_ID: app.Data.userInfo.User_ID,
   //       User_PayPassword: User_PayPassWord,
   //       mch_appid: 'wxa087ce51079f066e',
   //       paySecret: 'sUiYuAnTeChNoLoGyHtTpSwWwLs293cC',
   //       mch_id: '1437610002',
   //       bank_code: bank_code,
   //       amount: _data.inputMoney,
   //       desc: '提现到银行卡',
   //       enc_bank_no: enc_bank_no,
   //       enc_true_name: enc_true_name
   //    }, function(res) {
   //       wx.hideLoading()
   //       if (res.code == 0) {
   //          _this.readyParam2(-_data.inputMoney, enc_true_name)
   //       } else {
   //          co.toastAllText(res.msg)
   //       }
   //    })
   // },

   // //资金流水记录(提现到微信)
   // readyParam1: function(money) {
   //    var param = {
   //       User_ID: app.Data.userInfo.User_ID,
   //       Abstract: '提现到用户微信账户上',
   //       Payer: 'User_' + app.Data.userInfo.User_ID,
   //       Money: money,
   //       Pay_From: '91_pay',
   //       Pay_To: 'wx_pay',
   //       To_Name: app.Data.userInfo.ID_Card_Name
   //    }
   //    _this.addMoneyRecord(param)
   // },

   // //资金流水记录(提现到银行)
   // readyParam2: function(money, name) {
   //    var param = {
   //       User_ID: app.Data.userInfo.User_ID,
   //       Abstract: '提现到法人银行卡账户上',
   //       Payer: 'User_' + app.Data.userInfo.User_ID,
   //       Money: money,
   //       Pay_From: '91_pay',
   //       Pay_To: 'bank_card',
   //       To_Name: name
   //    }
   //    _this.addMoneyRecord(param)
   // },

   // //添加资金流水记录
   // addMoneyRecord: function(param) {
   //    co.addMoneyRecord_User(param, function(res) {
   //       console.log('res11111===', res)
   //       // console.log('res.data.New_Balance',res.data.New_Balance)
   //       if (res.code == 0) {
   //          app.Data.userInfo.User_Balance = res.data.New_Balance
   //          _this.setData({
   //             money: app.Data.userInfo.User_Balance
   //          })
   //          co.navigateBack({}, 1000)
   //       }
   //       wx.hideLoading()
   //       co.toastAllText(res.msg)
   //    })
   // }
})